package com.example.service;

import com.example.entity.CheckNumber;
import com.example.entity.Post;
import com.example.mapper.CheckNumberMapper;
import com.example.mapper.PostMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CheckNumberService
{

    @Resource
    private CheckNumberMapper checkNumberMapper;

    public void add(CheckNumber checkNumber) {
        checkNumberMapper.insert(checkNumber);
    }

    public void deleteById(Integer id) {
        checkNumberMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            this.deleteById(id);
        }
    }

    public void updateById(CheckNumber checkNumber) {
        List<CheckNumber> theCheckNumber=selectAll(checkNumber);
        /**空的时候只调用add函数*/
        if(theCheckNumber.size()==0)
        {
            add(checkNumber);
            return;
        }
        System.out.println("获得的ID是"+theCheckNumber.get(0).getId());
        boolean flag=false;
        try {
            if (checkNumber.getCheckNumber().equals(1)) {
                theCheckNumber.get(0).setCheckNumber(theCheckNumber.get(0).getCheckNumber() + 1);
                flag = true;
            }
        }
        catch (Exception e)
        {
                //bug！忽略
        }
        try{
            if (checkNumber.getMapNumber().equals(1)) {
                theCheckNumber.get(0).setMapNumber(theCheckNumber.get(0).getMapNumber() + 1);
                flag = true;
            }
        }
        catch (Exception e)
        {
            //bug!忽略
        }
        if(flag)
        {
            checkNumberMapper.updateById(theCheckNumber.get(0));
        }
        else
        {
            checkNumberMapper.updateById(checkNumber);
        }
    }

    public CheckNumber selectById(Integer id) {
        return checkNumberMapper.selectById(id);
    }

    public List<CheckNumber> selectAll(CheckNumber checkNumber) {
        return checkNumberMapper.selectAll(checkNumber);
    }

    public PageInfo<CheckNumber> selectPage(CheckNumber checkNumber, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CheckNumber> checkNumberList = checkNumberMapper.selectAll(checkNumber);
        return PageInfo.of(checkNumberList);
    }
}
